package cn.chitanda.weather.ui.Weather

import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.recyclerview.widget.DiffUtil
import androidx.recyclerview.widget.ListAdapter
import androidx.recyclerview.widget.RecyclerView
import cn.chitanda.weather.R
import cn.chitanda.weather.data.weather.Weather
import kotlinx.android.synthetic.main.viewpager_item.view.*

class ViewPagerAdapter : ListAdapter<Weather, VPViewHolder>(DIFFCALLBACK) {
    object DIFFCALLBACK : DiffUtil.ItemCallback<Weather>() {
        override fun areItemsTheSame(oldItem: Weather, newItem: Weather): Boolean {
            return oldItem.basic.cid == newItem.basic.cid
        }

        override fun areContentsTheSame(oldItem: Weather, newItem: Weather): Boolean {
            return oldItem == newItem
        }

    }

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): VPViewHolder {
        val view =
            LayoutInflater.from(parent.context).inflate(R.layout.viewpager_item, parent, false)
        return VPViewHolder(view)
    }

    override fun onBindViewHolder(holder: VPViewHolder, position: Int) {
//        holder.itemView.weather_TB.title = getItem(position).location
        val weather = getItem(position)
        holder.itemView.currentTmp.text = weather.now._tmp
        holder.itemView.nowCond.text = weather.now.cond_txt
        holder.itemView.forecastTmp.text = weather.forecast[0].tmp
        holder.itemView.forecast1.text = weather.forecast[0].toString()
        holder.itemView.forecast2.text = weather.forecast[1].toString()
        holder.itemView.forecast3.text = weather.forecast[2].toString()
    }
}

class VPViewHolder(itemView: View) : RecyclerView.ViewHolder(itemView)